﻿Imports VN.INS.BL.BC
Imports System.Data
Imports System.IO
Imports Microsoft.Office.Interop

Partial Class Devida_RankingAvanceFinanMon
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not (Page.IsPostBack)) Then
            ListaPeriodoInicio()
        End If
    End Sub

    Public Sub ListaPeriodoInicio()
        Dim objGeneral As New GeneralBC
        Dim objDt As DataTable
        Try
            objGeneral = New GeneralBC
            objDt = New DataTable
            'carga de combo periodo
            objDt = objGeneral.ListaPeriodoReporte("0", "DESC", Session("Año"))
            ddlPeriodo.DataSource = objDt
            ddlPeriodo.DataTextField = "DESCRIPCION"
            ddlPeriodo.DataValueField = "PERIODO"
            ddlPeriodo.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnGenerar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnGenerar.Click
        'carga reprote de ranking
        Dim objPPER As PPERBC
        Dim objData As DataTable
        objPPER = New PPERBC
        objData = New DataTable
        objData = objPPER.ListaPPER("")
        Dim objReporte As ReporteBC
        Dim objData1 As DataTable
        objReporte = New ReporteBC
        objData1 = New DataTable
        objData1 = objReporte.ReporteRanking(ddlPeriodo.SelectedValue.ToString)
        ViewState("Data") = objData1
        gvRanking.DataSource = objData
        gvRanking.DataBind()
        Me.DivReporte.Style("display") = "inline"
    End Sub

    Protected Sub gvRanking_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRanking.RowDataBound
        'formato de grilla y llenado de anidados
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hfPPER As HiddenField = CType(e.Row.FindControl("hfPPER"), HiddenField)
            If hfPPER.Value = "PPE-0001" Then
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(146, 208, 80)
            End If
            If hfPPER.Value = "PPE-0002" Then
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(204, 192, 218)
            End If
            If hfPPER.Value = "PPE-0003" Then
                e.Row.Cells(0).BackColor = Drawing.Color.FromArgb(183, 222, 232)
            End If
            e.Row.Cells(0).ForeColor = Drawing.Color.FromArgb(0, 0, 0)
            Dim IDpper As String = hfPPER.Value.ToString
            Dim vst As DataView = New DataView(ViewState("Data"), "PPER='" + IDpper + "'", "", DataViewRowState.CurrentRows)
            Dim gdv As GridView = e.Row.Cells(1).FindControl("gvDetalle")
            gdv.DataSource = vst
            gdv.DataBind()
        End If
    End Sub

    Private _total1, _total2, _total3 As Decimal
    Private _totalPresMan1, _totalPresMan2, _totalPresMan3 As Decimal
    Private _totalMonGir1, _totalMonGir2, _totalMonGir3 As Decimal
    Private _totalE1, _totalE2, _totalE3 As Decimal
    Private n As Int16 = 0
    Public Sub RDB(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            Dim _Row As System.Data.DataRowView = e.Row.DataItem
            Select Case _Row.Item(0).ToString
                Case "PPE-0001"
                    Dim PresMan As Decimal = Convert.ToDecimal(_Row.Item(2).ToString)
                    Dim MonGir As Decimal = Convert.ToDecimal(_Row.Item(3).ToString)                    
                    Dim price1 As Decimal = Convert.ToDecimal(_Row.Item(6).ToString)
                    Dim AvanGir As Decimal = Convert.ToDecimal(_Row.Item(7).ToString)
                    Me._totalPresMan1 = Me._totalPresMan1 + PresMan
                    Me._totalMonGir1 = Me._totalMonGir1 + MonGir
                    Me._totalE1 = Me._totalE1 + AvanGir
                    Me._total1 = Me._total1 + price1
                Case "PPE-0002"
                    Dim PresMan As Decimal = Convert.ToDecimal(_Row.Item(2).ToString)
                    Dim MonGir As Decimal = Convert.ToDecimal(_Row.Item(3).ToString)
                    Dim price2 As Decimal = Convert.ToDecimal(_Row.Item(6).ToString)
                    Dim AvanGir As Decimal = Convert.ToDecimal(_Row.Item(7).ToString)
                    Me._totalPresMan2 = Me._totalPresMan2 + PresMan
                    Me._totalMonGir2 = Me._totalMonGir2 + MonGir
                    Me._totalE2 = Me._totalE2 + AvanGir
                    Me._total2 = Me._total2 + price2
                Case "PPE-0003"
                    Dim PresMan As Decimal = Convert.ToDecimal(_Row.Item(2).ToString)
                    Dim MonGir As Decimal = Convert.ToDecimal(_Row.Item(3).ToString)
                    Dim price3 As Decimal = Convert.ToDecimal(_Row.Item(6).ToString)
                    Dim AvanGir As Decimal = Convert.ToDecimal(_Row.Item(7).ToString)
                    Me._totalPresMan3 = Me._totalPresMan3 + PresMan
                    Me._totalMonGir3 = Me._totalMonGir3 + MonGir
                    Me._totalE3 = Me._totalE3 + AvanGir
                    Me._total3 = Me._total3 + price3
            End Select
        ElseIf e.Row.RowType = DataControlRowType.Footer Then
            e.Row.BackColor = Drawing.Color.FromArgb(255, 255, 255)
            n = n + 1
            Select Case n
                Case 1
                    e.Row.Cells(1).Text = Me._totalPresMan1.ToString("#,##0")
                    e.Row.Cells(2).Text = Me._totalMonGir1.ToString("#,##0")
                    e.Row.Cells(3).Text = (Me._totalMonGir1 / Me._totalPresMan1).ToString("#,##0 %")                    
                    e.Row.Cells(5).Text = Me._total1.ToString
                    e.Row.Cells(6).Text = Me._totalE1.ToString("#,##0")
                Case 2
                    e.Row.Cells(1).Text = Me._totalPresMan2.ToString("#,##0")
                    e.Row.Cells(2).Text = Me._totalMonGir2.ToString("#,##0")
                    e.Row.Cells(3).Text = (Me._totalMonGir2 / Me._totalPresMan2).ToString("#,##0 %")
                    e.Row.Cells(5).Text = Me._total2.ToString
                    e.Row.Cells(6).Text = Me._totalE2.ToString("#,##0")
                Case 3
                    e.Row.Cells(1).Text = Me._totalPresMan3.ToString("#,##0")
                    e.Row.Cells(2).Text = Me._totalMonGir3.ToString("#,##0")
                    e.Row.Cells(3).Text = (Me._totalMonGir3 / Me._totalPresMan3).ToString("#,##0 %")
                    e.Row.Cells(5).Text = Me._total3.ToString
                    e.Row.Cells(6).Text = Me._totalE3.ToString("#,##0")
            End Select
            e.Row.Cells(0).ColumnSpan = 2
            e.Row.Cells(0).Text = "Totales"
            e.Row.Font.Bold = True            
            e.Row.ForeColor = Drawing.Color.Black
            e.Row.HorizontalAlign = HorizontalAlign.Right
            e.Row.Cells(0).HorizontalAlign = HorizontalAlign.Center
            e.Row.Cells(4).Text = "1.00"
            e.Row.Cells(8).Visible = False
        End If
    End Sub

    Public Sub Exportar()
        Dim CabeceraTabla As String = "<Table style='background-color: #BDD2E8;' >" + _
                                    "<tr><td align='center' style='bold;border: 1px solid #666666'>PPER</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>Ranking</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>Monitoreador</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>Presupuesto manejado (A)</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'> Monto girado acumulado (B)</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>%  Avance girado C=(A/B)</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>Peso relativo D=(A/Total)</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>N° Actividades</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>N° ejecutores</td>" + _
                                    "<td align='center' style='bold;border: 1px solid #666666'>Desempeño (C x D)</td>" + _
                                    "</tr>" + _
                                    "</Table>"
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.InnerHtml = CabeceraTabla
        form.Controls.Add(gvRanking)
        pagina.RenderControl(htw)
        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=ReporteRanking_" + Now.Date.Year.ToString + Now.Date.Month.ToString + Now.Date.Day.ToString + Now.Hour.ToString + Now.Minute.ToString + Now.Second.ToString + ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.End()
    End Sub

    Protected Sub btnExportar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnExportar.Click
        Exportar()
    End Sub

    Protected Sub btnCerrar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCerrar.Click
        'salir
        Response.Redirect("Inicio.aspx")
    End Sub
End Class
